package com.myday.system.controller;

import com.alibaba.fastjson2.JSONObject;
import com.myday.system.domain.dto.MessageDTO;
import com.myday.system.domain.vo.MessageVO;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @author: xiaofengs
 * @description :
 * @date: 2024/5/19 23:26
 * @version: 1.0
 */

@RestController
@RequestMapping("/websocket")
public class ChatroomController extends BaseController {

    @Autowired
    private  WebSocketServerController webSocketServerController;
    @GetMapping("/sendAllWebSocket")
    @PreAuthorize("@ss.hasPermi('chatroom:index')")
    public AjaxResult test() {
        MessageVO messageVO = new MessageVO();
        messageVO.setType("system");
        messageVO.setContent("这是websocket全体发送！");
        webSocketServerController.sendAllMessage(messageVO.getContent());
        return success((Object) JSONObject.toJSONString(messageVO));
    }

    @PostMapping("/sendOneWebSocket")
    @PreAuthorize("@ss.hasPermi('chatroom:index')")
    public AjaxResult sendOneWebSocket(@RequestBody MessageDTO messageDTO) {
        webSocketServerController.sendOneMessage(messageDTO);
        System.out.println(messageDTO);
        MessageVO messageVO = new MessageVO();
        messageVO.setType("user");
        messageVO.setContent(messageDTO.getMessage());
        return success((Object) JSONObject.toJSONString(messageVO));
    }
}
